//
//  DynamicIntersectionView.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 8/20/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "DynamicIntersectionView.h"
static const int NORTHBOUND_POSITION_X=0;
static const int NORTHBOUND_POSITION_Y=0;
static const int SOUTHBOUND_POSITION_X=0;
static const int SOUTHBOUND_POSITION_Y=0;
static const int EASTBOUND_POSITION_X=0;
static const int EASTBOUND_POSITION_Y=0;
static const int WESTBOUND_POSITION_X=0;
static const int WESTBOUND_POSITION_Y=0;
static const int NORTHSOUTH_WIDTH=15;
static const int NORTHSOUTH_HEIGHT=24;
static const int WESTEAST_WIDTH=24;
static const int WESTEAST_HEIGHT=15;

@implementation DynamicIntersectionView
@synthesize signal=_signal;
@synthesize ring1ImageList=_ring1ImageList;
@synthesize ring2ImageList=_ring2ImageList;

- (void) dealloc
{
    [_signal release];
    [_ring1ImageList release];
    [_ring2ImageList release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/



- (id) initWithSignal:(Signal *)signal
{
    self.signal=signal;
    self=[self initWithFrame:CGRectMake(60,85,200,200)];
    UIImage *image=[UIImage imageNamed: @"intbak.png"];
    [self setImage:image];
//    [self.layer setBorderColor: [[UIColor blueColor] CGColor]];
//    [self.layer setBorderWidth: 2.0];
    self.ring1ImageList=[[[NSMutableArray alloc] init] autorelease];
    self.ring2ImageList=[[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<signal.ring1.phaseList.count; i++) {
        SignalPhase *phase=[signal.ring1.phaseList objectAtIndex:i];
        //        NSString *phaseMovement=phase.phaseMovement;
        
        NSString* phaseMovement=phase.phaseMovement;
        NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
        NSRange commaRange=[phaseMovement rangeOfString:@","];
        while (commaRange.location!=NSNotFound) {
            NSString *movement=[phaseMovement substringToIndex:commaRange.location];
            [movementList addObject:movement];
            phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
            commaRange=[phaseMovement rangeOfString:@","];
        }
        if (phaseMovement.length>0) {
            [movementList addObject:phaseMovement];
        }
        
        for (NSString *movement in movementList) {
            
            
            NSRange range = [movement rangeOfString:@"B"];
            
            //        NSString *imageName=nil;
            if (range.location != NSNotFound) {
                // found a colon
                if (range.location + 1 < [movement length]) {
                    NSString *directionName =[movement substringToIndex:range.location] ;
                    NSString *movementName=[movement substringFromIndex:(range.location + 1)];
                    for (int k=0; k<movementName.length; k++) {
                        NSString *movementCharacter=[movementName substringFromIndex:k];
                        movementCharacter=[movementCharacter substringToIndex:1];
                        NSString *imageName=[NSString stringWithFormat:@"%@%@.png",directionName,movementCharacter];
                        MovementImageView *movementView=[[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageName]]] autorelease];
                        movementView.phaseID=phase.phaseID;
                        movementView.ringID=@"1";
                        movementView.direction=directionName;
                        movementView.movement=movementCharacter;
                        movementView.imageName=imageName;
                        [movementView setFrameByDirection];
                        [self.ring1ImageList addObject:movementView];
                        [self addSubview:movementView];
                    }
                    
                    //                return nil;
                }else {
                    //                return nil;
                }
            }
        }
        
        
    }
    for (int i=0; i<signal.ring2.phaseList.count; i++) {
        SignalPhase *phase=[signal.ring2.phaseList objectAtIndex:i];
        //        NSString *phaseMovement=phase.phaseMovement;
        NSString* phaseMovement=phase.phaseMovement;
        NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
        NSRange commaRange=[phaseMovement rangeOfString:@","];
        while (commaRange.location!=NSNotFound) {
            NSString *movement=[phaseMovement substringToIndex:commaRange.location];
            [movementList addObject:movement];
            phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
            commaRange=[phaseMovement rangeOfString:@","];
        }
        if (phaseMovement.length>0) {
            [movementList addObject:phaseMovement];
        }
        
        for (NSString *movement in movementList) {
            
            
            NSRange range = [movement rangeOfString:@"B"];
            
            //        NSString *imageName=nil;
            if (range.location != NSNotFound) {
                // found a colon
                if (range.location + 1 < [movement length]) {
                    NSString *directionName =[movement substringToIndex:range.location] ;
                    NSString *movementName=[movement substringFromIndex:(range.location + 1)];
                    for (int k=0; k<movementName.length; k++) {
                        NSString *movementCharacter=[movementName substringFromIndex:k];
                        movementCharacter=[movementCharacter substringToIndex:1];
                        NSString *imageName=[NSString stringWithFormat:@"%@%@.png",directionName,movementCharacter];
                        MovementImageView *movementView=[[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageName]]] autorelease];
                        movementView.phaseID=phase.phaseID;
                        movementView.ringID=@"2";
                        movementView.direction=directionName;
                        movementView.movement=movementCharacter;
                        movementView.imageName=imageName;
                        [movementView setFrameByDirection];
                        [self.ring2ImageList addObject:movementView];
                        [self addSubview:movementView];
                    }
                    
                    //                return nil;
                }else {
                    //                return nil;
                }
            }
        }
        
        
    }
    
    return self;
}



- (void) setPhaseImageColor:(NSString *) phaseID ring:(int)ring color:(int) color;
{
    switch (ring) {
        case 1:
        {
            switch (color) {
                case DGREEN:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"GREEN_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                case DYELLOW:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"YELLOW_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                    
                }
                    break;
                case DRED:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                    
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (color) {
                case DGREEN:
                {
                    
                    for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"GREEN_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                }
                    break;
                case DYELLOW:
                {
                    for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"YELLOW_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                case DRED:
                {
                    for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
    
}


@end
